<template>
  <div class="cart">
    <van-nav-bar
      title="标题"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <div class="goods-list" v-if="list.length > 0">
      <div class="goods" v-for="(data, i) in list" :key="data._id + i">
        <van-checkbox v-model="data.checked" />
        <img :src="data.product.coverImg" alt="" class="goods-img" />
        <div class="goods-info">
          <p>{{ data.product.name }}</p>
          <span>￥{{ data.product.price }}</span>
          <span @click="updetacart(data.product._id, 1)">+</span>
          {{ data.quantity }}
          <span @click="data.quantity > 1 && updetacart(data.product._id, -1)"
            >-</span
          >
          <span v-show="data.checked" @click="del(data._id)">删除</span>
        </div>
      </div>
    </div>
    <van-empty v-else description="购物车空空如也" />

    <van-submit-bar
      :price="sumPrice * 100"
      button-text="提交订单"
      @submit="onSubmit"
    >
      <van-checkbox v-model="checked">全选</van-checkbox>
      <van-icon name="delete-o" v-show="checked" @click="dels" />
    </van-submit-bar>
  </div>
</template>

<script>
import {
  getcartapi,
  addcartapi,
  delcartapi,
  delallcartapi,
  saveorderapi,
} from "../../api/cart";
export default {
  data() {
    return {
      list: [],
    };
  },
  computed: {
    // 全选功能实现
    checked: {
      set(flag) {
        this.list.map((item) => this.$set(item, "checked", flag));
      },
      get() {
        if (this.list.length == 0) {
          return false;
        } else {
          return (
            this.list.length == this.list.filter((item) => item.checked).length
          );
        }
      },
    },
    // 总价计算
    sumPrice() {
      return this.list
        .filter((item) => {
          return item.checked;
        })
        .reduce(function (pre, cur) {
          return pre + cur.product.price * cur.quantity;
        }, 0);
    },
    // 统计选中项
    selectgoods() {
      let selectlist = [];
      this.list.filter((item) => {
        if (item.checked) {
          selectlist.push({
            quantity: item.quantity,
            product: item._id,
            price: item.product.price,
          });
        }
      });
      return selectlist;
    },
  },
  watch: {},

  methods: {
    //返回上一页
    onClickLeft(){
      this.$router.go(-1);
      // location.reload();
    },
    // 获取购物车列表
    async getcart() {
      const result = await getcartapi();
      console.log(result);
      this.list = result;
    },
    // 修改商品数量
    async updetacart(id, num) {
      const result = await addcartapi(id, num);
      console.log(result);
      this.list.forEach((data) => {
        if (data.product._id == id) {
          data.quantity = data.quantity + num;
        }
      });
    },
    // 删除单个产品
    async del(id) {
      const result = await delcartapi(id);
      console.log(result);
      this.getcart();
    },
    // 删除多个产品
    async dels() {
      let ids = [];
      this.selectgoods.forEach((item) => {
        ids.push(item.product);
      });
      const result = await delallcartapi(ids);
      console.log(result);
      this.getcart();
    },
    // 提交购物车
    async onSubmit() {
      const receiver = this.$store.state.address.receiver;
      const regions = this.$store.state.address.regions;
      const address = this.$store.state.address.address;
      console.log(receiver);
      console.log(regions);
      console.log(address);
      if (!receiver || !regions || !address) {
        this.$router.push("/address");
      } else {
        const orderDetails = this.selectgoods;
        const result = await saveorderapi({
          receiver,
          regions,
          address,
          orderDetails,
        });
        console.log(result);
        this.$router.push("/order");
      }
    },
  },
  created() {
    this.getcart();
  },
  mounted() {},
  components: {},
};
</script>
<style scoped>
.goods-list {
  padding-bottom: 50px;
}
.goods {
  display: flex;
  height: 100px;
  width: 100%;
  justify-content: space-around;
  align-content: center;
  margin-bottom: 15px;
}
.goods-img {
  flex: 1;
  margin: 0 auto;
  height: 80px;
  border: 1px solid #ccc;
}
.goods-info {
  flex: 3;
}
</style>
